# -*- coding:utf-8 -*-

from app.tools.redis_op import Operator
from app.models.site_setting import CarAttribute, AttrBrand, AttrCarModel


def init_attr():
	op = Operator()
	c = CarAttribute.query.get(1)
	brands = AttrBrand.query.order_by(AttrBrand.sort).all()
	car_models = AttrCarModel.query.order_by(AttrCarModel.sort).all()
	
	type = {}
	for i in car_models:
		type[i.id] = i.name
	op.hsetall('car_type', type)

	brand = {}
	for i in brands:
		brand[i.id] = i.name
	op.hsetall('brand', brand)

	gearbox = {}
	for i,j in enumerate(CarAttribute.json_load(c.gearbox)['gearbox']):
		gearbox[i] = j
	op.hsetall('gearbox', gearbox)

	standard = {}
	for i,j in enumerate(CarAttribute.json_load(c.standard)['standard']):
		standard[i] = j
	op.hsetall('standard', standard)

	color = {}
	for i,j in enumerate(CarAttribute.json_load(c.color)['color']):
		color[i] = j
	op.hsetall('color', color)
